<?php
/**
 *
 * 交易
 * User:伽偌
 * QQ：2227232928
 * Date: 16/7/27
 * Time: 下午11:05
 */

namespace Tool\Controller;
use Think\Controller;
use Think\Page;

class BuyController extends CommonController{
    /**
     * 饲料记录
     */
    public function buyFeedLog(){
        $log = M('user_finance as finance')->field('member.phone,finance.*')->join('inner join '.C('DB_PREFIX').'member as member on member.id = finance.userid')->where('type = 2')->limit(100)->select();
//        print_r($log);
        $this->assign('log',$log);
        $this->display();
    }

    public function delFeedLog(){
        if(IS_AJAX){
            $id = I('id','','intval');
            if($id){
                $res = M('user_finance')->delete($id);
                if($res){
                    $this->ajaxReturn(array('status'=>1));
                }
            }
        }
    }


    public function nasty()
    {


        $pagenumber = 20;


        $uid=I("get.id");
        $day=I("get.day");
        $model=M("brooding_log");

        $con=array('type'=>7);


        if ($uid)
        {
            $con["userid"] = $uid;
            $con['animal_on'] = "to-" . $uid;
        }
        if($day)
        {
            $today=strtotime(date("Y-m-d"));
            $con["create_time"] = array("between",array($today,$today+86400));
        }

		$pagenum=$_GET['p']?0:1;


        $member_count =$model->where($con)->count();
		 
		 
        $page = new Page($member_count,$pagenumber);
        
		 
        $show = $page->show();
		 
        $member_list =$model->where($con)->page($pagenum)->limit($pagenumber)->order("id desc")->select();
        $this->assign('member_count', $member_count);
        $this->assign('list', $member_list);
        $this->assign('page', $show);
        $this->display();
    }

    public function addanimals()
    {

        if (IS_POST)
        {
            $post = I('post.');
            $toUserId=$post['userid'];
            $type=$post['type'];

            $maxout = C('maxoutput');
            if ($type) $maxout = C('supermax');

            M('member')->where(array('id' => $toUserId))->setField('active', 1);

            $buyNumber = M('user_animal')->where( array('userid'=>$toUserId   ))->count();
            $animalNo =  strval( $buyNumber + 1);  //机器编号
            $lifeCycle = C('lifeCycle');//生命周期
            $expiredTime = time() + $lifeCycle * 86400;
            $animalResult = M('user_animal')->add(array(
                'userid' => $toUserId,
                'animal_on' => $animalNo,
                'create_time' => strtotime(date('Y-m-d', time()) . ' 00:00:00'),
                'expired_time' => $expiredTime,
                'maxoutput'=>$maxout,
                'type'=>$type
            ));

            /***************************************************/
            //赠送咖啡
            $reward=I("post.naicha",0,"intval");
            if ($reward > 0)
            {
                $brooding = array(
                    'userid' => $toUserId,
                    'animal_on' => "to-0" ,  //平台赠送
                    'number' => $reward,
                    'create_time' => time(),
                    'type' => 7   //系统奖励
                );
                M('brooding_log')->add($brooding);
                M('member')->where(array('id' => $toUserId))->setInc('currency', $reward);//长奖励
            }



            if($animalResult)
                $this->success('赠送成功');
            else
                $this->error('赠送失败');
            die();
        }

        $this->display();
    }


    public function animals()
    {
        $animals=M('user_animal')->order('id desc')->select();
        $this->assign("lists",$animals);
        $this->display();
    }
    public function aniinfo()
    {

        if(IS_POST){
            $post = I('post.');
            $member =  M('user_animal')->where(array('id'=>$post['id']))->find();
            if(!$member){ die("<script>alert('机器不存在');history.go(-1);</script>"); }

            if($post['producttime'] ){  $post['producttime']=strtotime($post['producttime']) ; }

            $ret =  M('user_animal')->where(array('id'=>$post['id']))->save($post);

            if ($ret)
            {
                die("<script>alert('修改成功');history.go(-1);</script>");
            } else
            {
                die("<script>alert('修改失败');history.go(-1);</script>");
            }

        }


        $id = I('get.id');
        $m = M('user_animal')->where(array('id'=>$id))->find();
        if(!$m){

            die("<script>alert('机器不存在');history.go(-1);</script>");
        }

        $this->assign('member',$m);
        $this->display();

    }



    /**
     * 激活码
     */
    public function buyActiveCodeLog(){
        $log = M('user_finance as finance')->field('member.phone,finance.*')->join('inner join '.C('DB_PREFIX').'member as member on member.id = finance.userid')->where('type = 3')->limit(100)->select();
//        print_r($log);
        $this->assign('log',$log);
        $this->display();
    }
    public function delActiveCodeLog(){
        if(IS_AJAX){
            $id = I('id','','intval');
            if($id){
                $res = M('user_finance')->delete($id);
                if($res){
                    $this->ajaxReturn(array('status'=>1));
                }
            }
        }
    }

    /**
     * 购买鸡记录
     */
    public function buyAnimalLog(){
        $log = M('user_finance as finance')
            ->field('member.phone,finance.*')
            ->join('inner join '.C('DB_PREFIX').'member as member on member.id = finance.userid')
            ->where('type = 1')
            ->order('finance.id desc')
            ->limit(200)->select();
//        print_r($log);
        $this->assign('log',$log);
        $this->display();
    }

    public function delAnimalLog(){
        if(IS_AJAX){
            $id = I('id','','intval');
            if($id){
                $res = M('user_finance')->delete($id);
                if($res){
                    $this->ajaxReturn(array('status'=>1));
                }
            }
        }
    }

    public function  Animalbrood()
    {
        $log = M('brooding_log')->order(" id desc ")->limit(4900)->select();
        $this->assign('log', $log);
        $this->display();
    }

    public function  market()
    {
        $log = M('market')->limit(300)->order("id desc")->select();
        $this->assign('log', $log);
        $this->display();
    }
    /**
     * 鸡场
     */
    public function buyAnimal(){
        $log = M('user_animal as animal')->field('member.phone,animal.*')->join('inner join '.C('DB_PREFIX').'member as member on member.id = animal.userid')->limit(100)->select();
        $this->assign('log',$log);
        $this->display();
    }
    public function delAnimal(){
        if(IS_AJAX){
            $id = I('id','','intval');
            if($id){
                $res = M('user_animal')->delete($id);
                if($res){
                    $this->ajaxReturn(array('status'=>1));
                }
            }
        }
    }
    /**
     * 奖金记录
     */
    public function bonusLog(){
        $log = M('bonus_log as bonus_log')->field('bonus_log.*,member.phone,member_to.phone as tophone')
            ->join('inner join '.C('DB_PREFIX').'member as member on member.id = bonus_log.userid')
            ->join('inner join '.C('DB_PREFIX').'member as member_to on member_to.id = bonus_log.touserid')
            ->limit(100)
            ->select();
        $this->assign('log',$log);
        $this->display();
    }
    public function delBonusLog(){
        if(IS_AJAX){
            $id = I('id','','intval');
            if($id){
                $res = M('bonus_log')->delete($id);
                if($res){
                    $this->ajaxReturn(array('status'=>1));
                }
            }
        }
    }

}
